package com.xiaoying.server.publish.impl;

import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.BlockingQueue;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import redis.clients.jedis.Jedis;

import com.xiaoying.server.publish.Handler;
import com.xiaoying.server.publish.entity.FileInfo;
import com.xiaoying.server.publish.entity.UploadResult;
import com.xiaoying.server.publish.servlet.Dispatcher;
import com.xiaoying.server.publish.upload.UploadFile;
import com.xiaoying.server.publish.utils.RedisUtil;

public class ExceptionHandler implements Handler,Runnable {
	 private static Log log = LogFactory.getFactory().getInstance(ExceptionHandler.class);

    private BlockingQueue<FileInfo> requestQueue;
    private BlockingQueue<UploadResult> responseQueue;
	
	@Override
	public void init() throws Exception {		
		
	}

	@Override
	public void handle(BlockingQueue<FileInfo> reqQ,
			BlockingQueue<UploadResult> resQ) {
        this.requestQueue = reqQ;
        this.responseQueue = resQ;
        Thread proc = new Thread(this);
        proc.start();
	}
	
	
	/**
	 * 处理异常退出的上传
	 * @author ZJD
	 * @param
	 */
	@Override
	public void run() {
		Jedis redis = RedisUtil.getRedisInstance();		
		String fuzzykey = RedisUtil.PREFIX_XY_BACKUP_UPLOAD_FILEINFO+"*";    	
    	Set<String> set = redis.keys(fuzzykey);    	
    	Iterator<String> iterator = set.iterator();
    	int count=0;
    	while(iterator.hasNext()){
    		log.info("收到上次异常退出的1个结果，放入队列了");
    		String key = iterator.next();    		
    		int id = Integer.valueOf(redis.hget(key,"id"));
    		String fileName = redis.hget(key,"fileName");
    		String filePath = redis.hget(key,"filePath");
    		long fileSize = Long.valueOf(redis.hget(key,"fileSize"));    		
    		String fileSuffix = redis.hget(key,"fileSuffix");
    		short fileType = Short.valueOf(redis.hget(key,"fileType"));
    		int uploadCount = Integer.valueOf(redis.hget(key,"uploadCount"));
    		String receiveDt = redis.hget(key,"receiveDt");  
//    		System.out.println(++count);
    		FileInfo fileInfo = new FileInfo(id,fileName,filePath,
    				fileSize,fileSuffix,fileType,uploadCount,receiveDt);
    		this.requestQueue.add(fileInfo);
    	}
		
	}
	
	

}
